### Create directories

in.dir = '/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/expCLIMyearlysummary/'
setwd(in.dir)
out.dir = '/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/expCLIM/'

# Load libraries

library('climates')
library('SDMTools')

### List all yearly summary files

maxfiles = list.files(in.dir,recursive=T,full.names=T,pattern='meanmax')
minfiles = list.files(in.dir,recursive=T,full.names=T,pattern='meanmin')

### Create blank objects to bind data to

for (h in c(1:12))

	{
	
	assign(paste('maxmonthsummary',sprintf('%02i',h),sep=''),NULL)
	assign(paste('minmonthsummary',sprintf('%02i',h),sep=''),NULL)
	
	}
	
# Close loop

for (i in c(1:30)) # This loop will extract each individual month (1 column) from the yearly summary, and bind them into a separate dataframe

	{
	
	max.data = read.csv(maxfiles[i], header=T)
	min.data = read.csv(minfiles[i], header=T)
	
		for (j in c(1:12))
		
			{
			
			if(j==1) 
			{
			maxmonthsummary01 = cbind(maxmonthsummary01,max.data[,j])
			minmonthsummary01 = cbind(minmonthsummary01,min.data[,j])
			}
			if(j==2) 
			{
			maxmonthsummary02 = cbind(maxmonthsummary02,max.data[,j])
			minmonthsummary02 = cbind(minmonthsummary02,min.data[,j])
			}
			if(j==3) 
			{
			maxmonthsummary03 = cbind(maxmonthsummary03,max.data[,j])
			minmonthsummary03 = cbind(minmonthsummary03,min.data[,j])
			}
			if(j==4) 
			{
			maxmonthsummary04 = cbind(maxmonthsummary04,max.data[,j])
			minmonthsummary04 = cbind(minmonthsummary04,min.data[,j])
			}
			if(j==5) 
			{
			maxmonthsummary05 = cbind(maxmonthsummary05,max.data[,j])
			minmonthsummary05 = cbind(minmonthsummary05,min.data[,j])
			}
			if(j==6) 
			{
			maxmonthsummary06 = cbind(maxmonthsummary06,max.data[,j])
			minmonthsummary06 = cbind(minmonthsummary06,min.data[,j])
			}
			if(j==7) 
			{
			maxmonthsummary07 = cbind(maxmonthsummary07,max.data[,j])
			minmonthsummary07 = cbind(minmonthsummary07,min.data[,j])
			}
			if(j==8) 
			{
			maxmonthsummary08 = cbind(maxmonthsummary08,max.data[,j])
			minmonthsummary08 = cbind(minmonthsummary08,min.data[,j])
			}
			if(j==9) 
			{
			maxmonthsummary09 = cbind(maxmonthsummary09,max.data[,j])
			minmonthsummary09 = cbind(minmonthsummary09,min.data[,j])
			}
			if(j==10) 
			{
			maxmonthsummary10 = cbind(maxmonthsummary10,max.data[,j])
			minmonthsummary10 = cbind(minmonthsummary10,min.data[,j])
			}
			if(j==11) 
			{
			maxmonthsummary11 = cbind(maxmonthsummary11,max.data[,j])
			minmonthsummary11 = cbind(minmonthsummary11,min.data[,j])
			}
			if(j==12) 
			{
			maxmonthsummary12 = cbind(maxmonthsummary12,max.data[,j])
			minmonthsummary12 = cbind(minmonthsummary12,min.data[,j])
			}
			
		}
		
		cat(i,'\n')	
		
	}	
	
# Close loop

# Find the mean for each cell across the 39 months

meanmaxmonth01 = rowMeans(maxmonthsummary01)
meanminmonth01 = rowMeans(minmonthsummary01)

meanmaxmonth02 = rowMeans(maxmonthsummary02)
meanminmonth02 = rowMeans(minmonthsummary02)

meanmaxmonth03 = rowMeans(maxmonthsummary03)
meanminmonth03 = rowMeans(minmonthsummary03)

meanmaxmonth04 = rowMeans(maxmonthsummary04)
meanminmonth04 = rowMeans(minmonthsummary04)

meanmaxmonth05 = rowMeans(maxmonthsummary05)
meanminmonth05 = rowMeans(minmonthsummary05)

meanmaxmonth06 = rowMeans(maxmonthsummary06)
meanminmonth06 = rowMeans(minmonthsummary06)

meanmaxmonth07 = rowMeans(maxmonthsummary07)
meanminmonth07 = rowMeans(minmonthsummary07)

meanmaxmonth08 = rowMeans(maxmonthsummary08)
meanminmonth08 = rowMeans(minmonthsummary08)

meanmaxmonth09 = rowMeans(maxmonthsummary09)
meanminmonth09 = rowMeans(minmonthsummary09)

meanmaxmonth10 = rowMeans(maxmonthsummary10)
meanminmonth10 = rowMeans(minmonthsummary10)

meanmaxmonth11 = rowMeans(maxmonthsummary11)
meanminmonth11 = rowMeans(minmonthsummary11)

meanmaxmonth12 = rowMeans(maxmonthsummary12)
meanminmonth12 = rowMeans(minmonthsummary12)

# Bind monthly summaries together

maxtobc = cbind(meanmaxmonth01,meanmaxmonth02,meanmaxmonth03,meanmaxmonth04,meanmaxmonth05,meanmaxmonth06,meanmaxmonth07,meanmaxmonth08,meanmaxmonth09,meanmaxmonth10,meanmaxmonth11,meanmaxmonth12)
mintobc = cbind(meanminmonth01,meanminmonth02,meanminmonth03,meanminmonth04,meanminmonth05,meanminmonth06,meanminmonth07,meanminmonth08,meanminmonth09,meanminmonth10,meanminmonth11,meanminmonth12)

# Run BC function

bcs = bioclim(tmin=mintobc,tmax=maxtobc,vois=c(1:6), cov=TRUE) # Modified this call

# Read in ASCII file

base.asc = read.asc('/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/STATIC/dem_WTplusbuffer_LatLong_WGS1984_250mres.asc')

# Write out microCLIM files

for (i in c(1,4,5,6))

	{
	
	base.asc[which(is.na(base.asc)==F)]=bcs[,i]
	
	write.asc.gz(base.asc, file=paste(out.dir,'expCLIM_0',i,sep=''))
	
	}
	
# Close loop
